// pagesA/pages/orderDetails/index.js
import {
  queryOrder,
  updateOrder,
  editUserBalance,
  getUserInfo
} from '../../../api/path/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    sts: '待付款',
    radio: 0,
    top: 0,
    goods: [],
    loading: false,
    checkedGoods: {
      freight: 12,
      minus: 100,
      coupon: 200,
      first: 0,
      num: 0,
      price: 0,
      list: [],
    },
    addressName: '',
    addressTel: '',
    addressAddress: '',
    user: ''
  },
  back: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  settlement: function (e) {
    this.setData({
      show: true
    })
  },
  onChange: function (event) {
    console.log(event.detail);
    this.setData({
      radio: event.detail,
    });
  },
  submitOrder: function (e) {
    this.setData({
      loading: true
    })

    // 判断使用的是余额还是微信
    if (this.data.radio == 2) {
      this.setData({
        loading: false
      })
      wx.showToast({
        title: '暂未开通！',
        icon: 'error'
      })
      return
    }
    if (this.data.user.balance < this.data.checkedGoods.price) {
      this.setData({
        loading: false
      })
      wx.showToast({
        title: '余额不足！',
        icon: 'error'
      })
      return
    }
    let sts = ''
    // 判断状态
    switch (this.data.sts) {
      case '待付款':
        sts = '待发货'
        break;
      case '待发货':
        sts = '待收货'
        break;
      case '待收货':
        sts = '待评价'
        break;
      case '待评价':
        sts = '已完成'
      case '退货':
        sts = '已完成'
    }

    let param = {
      "orderStatus": sts,
      "shippingAddress": this.data.addressAddress,
      "paymentMethod": "微信",
      "orderID": wx.getStorageSync('orderID')
    }
    let price = this.data.user.balance - this.data.checkedGoods.price
    console.log(price);
    // 修改订单
    updateOrder(param).then(res => {
      if (res.code == 1) {
        // 修改余额
        editUserBalance({
          id: this.data.user.userID,
          name: price
        }).then(result => {
          wx.showToast({
            title: '提交成功！',
            icon: 'none'
          })
          getUserInfo({
            id: this.data.user.userID
          }).then(info => {
            if (info.code == 1) {
              this.setData({
                user: info.data[0]
              })
              wx.setStorageSync('userinfo', JSON.stringify(info.data[0]))
            }
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          },2000)
        })
      } else {
        wx.showToast({
          title: '提交失败！',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          top: res.safeArea.top
        })
      }
    })
    queryOrder({
      id: wx.getStorageSync('orderID')
    }).then(res => {
      console.log(res);
      this.setData({
        goods: res.data.orderProductsList,
        addressName: res.data.userName,
        addressTel: res.data.tel,
        addressAddress: res.data.shippingAddress,
        sts: res.data.orderStatus,
      })

      // 计算 每一个 商品的 价格 和 数量
      var checkedGoods = {
        freight: 12,
        minus: 100,
        coupon: 50,
        first: 0,
        num: 0,
        price: 0,
        list: [],
      }
      res.data.orderProductsList.forEach(item => {
        console.log('item', item);
        checkedGoods.first += item.productPrice
        checkedGoods.price = item.productPrice - (checkedGoods.freight + checkedGoods.minus + checkedGoods.coupon)
        checkedGoods.list.push({
          price: item.productPrice,
          first: item.productPrice,
        })
      })
      this.setData({
        checkedGoods: checkedGoods
      })
    })
    // 获取最新的账号信息
    var user = JSON.parse(wx.getStorageSync('userinfo'))
    getUserInfo({
      id: user.userID
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          user: res.data[0]
        })
        wx.setStorageSync('userinfo', JSON.stringify(res.data[0]))
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})